package com.bionic.banking.logic;

import com.bionic.banking.core.entity.Country;
import com.bionic.banking.core.entity.Currency;
import com.bionic.banking.core.enums.BankAccountTypeEnum;
import com.bionic.banking.core.enums.BankTransactionTypeEnum;
import com.bionic.banking.core.enums.Gender;
import com.bionic.banking.dao.connection.LoggerLoader;
import com.bionic.banking.logic.controller.BankTransactionController;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;

public final class DomainUtil {

    private static final Logger logger = LoggerLoader.getLogger(BankTransactionController.class);
    
    public static List<Country> getCountries() {

        return Country.getCountries();
    }

    public static List<Currency> getCurrencies() {

        return Currency.getCurrencies();
    }

    public static Currency getCurrencyByCode(Integer code) {

        return Currency.getByCode(code);
    }

    public static Currency getCurrencyBySymbol(String symbol) {

        return Currency.getBySymbol(symbol);
    }

    public static Currency getCurrencyByName(String name) {

        return Currency.getByName(name);
    }

    public static Gender getGenderSignature(Boolean genderId) {

        return Gender.parse(genderId);
    }
    private static BankTransactionTypeEnum[] transactionTypes = null;

    public static BankTransactionTypeEnum[] getTransactionTypes() {
        if (transactionTypes == null) {
            transactionTypes = BankTransactionTypeEnum.values();
        }
        return transactionTypes;
    }
    private static BankAccountTypeEnum[] accountTypes = null;

    public static BankAccountTypeEnum[] getAccountTypes() {
        if (accountTypes == null) {
            accountTypes = BankAccountTypeEnum.values();
        }
        return accountTypes;
    }

    public static boolean checkAccountType(String type) {
        return BankAccountTypeEnum.contains(type);
    }

    public static boolean isCreditAccount(String name) {
        BankAccountTypeEnum type = BankAccountTypeEnum.parseString(name);
        return type.equals(BankAccountTypeEnum.CREDIT);
    }

    public static byte[] fileToByteArray(File file) throws FileNotFoundException {
        int len = (int)file.length();
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[len];
        try {
            for (int readNum; (readNum = fis.read(buf)) != -1;) {
                bos.write(buf, 0, readNum);
                System.out.println("read " + readNum + " bytes,");
            }
        } catch (IOException ex) {
            logger.error(ex);
        }
        byte[] bytes = bos.toByteArray();

        return bytes;
    }

    public static byte[] fileToByteArray(String filepath) throws FileNotFoundException {
        File file = new File(filepath);
        return fileToByteArray(file);
    }

    public static String byteArrayToString(byte[] imageByteArray) {
        return new String(imageByteArray);
    }

    public static BufferedImage byteArrayToImage(byte[] imageByteArray) throws IOException {
        
        if (imageByteArray == null) {
            return null;
        }
        final BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(imageByteArray));
        File file = new File("/portret.jpg");
        ImageIO.write(bufferedImage, "jpeg", file);
        return bufferedImage;
    }
    
    public static byte[] stringToBuffer(String imageBuffStr) throws UnsupportedEncodingException {       
        String[] strArray = imageBuffStr.split(",");
        byte[] bytes = new byte[strArray.length];        
        for (int i=0, len=bytes.length; i<len; i++) {
            int value = Integer.valueOf(strArray[i].trim());
            bytes[i] = (byte) value;  
        }        
        return bytes; 
    }
}